import pyautogui as pg
import os
import time
import pyperclip
import requests
import json
import psutil
import sys
import win32gui, win32com.client, win32api
import win32con
import win32process  
import ctypes
from pywinauto.application import Application

def enum_child_windows(hwnd, result):  
    result.append((hwnd, win32gui.GetWindowText(hwnd)))  

def set_foreground_window(hwnd):  
    _, pid = win32process.GetWindowThreadProcessId(hwnd)  
    ctypes.windll.user32.AllowSetForegroundWindow(pid)  
    shell = win32com.client.Dispatch("WScript.Shell")
    shell.SendKeys('%{1}')
    win32gui.SetForegroundWindow(hwnd)  



# hwnd = win32gui.FindWindow(None, "斯维尔清单计价 TH-BQ2016 学习版")
# if hwnd:
#     # win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)
#     print(f"窗口句柄为：{hwnd}")
# else:
#     print("未找到指定窗口")






# time.sleep(5)
# tips = win32gui.FindWindow(None, "提示")


# app = Application(backend='uia').connect(handle=tips)
# dlg = app.window(title=win32gui.GetWindowText(tips))  # 根据你的窗口标题进行调整  
# print(dlg.print_ctrl_ids())
hwnd = win32gui.FindWindow(None, "投标信息")
left, top, right, bottom = win32gui.GetWindowRect(hwnd)  
screen_width = win32api.GetSystemMetrics(win32con.SM_CXSCREEN)  
screen_height = win32api.GetSystemMetrics(win32con.SM_CYSCREEN)  
window_width = right - left  
window_height = bottom - top  

# 计算新位置  
new_left = (screen_width - window_width) // 2  
new_top = (screen_height - window_height) // 2  


set_foreground_window(hwnd)
pg.hotkey("tab")
pg.typewrite("1")
# 移动窗口  
win32gui.SetWindowPos(hwnd, win32con.HWND_TOP, new_left, new_top, 551, 471, 0)
pg.click((right - left) * (1 * (10 / 13)) + left, (bottom - top) * ((18.5 / 20) * 1) + top)

time.sleep(1)
pg.typewrite("1")
# 移动窗口  
win32gui.SetWindowPos(hwnd, win32con.HWND_TOP, new_left, new_top, 551, 451, 0)
pg.click((right - left) * (1 * (10 / 13)) + left, (bottom - top) * ((18.5 / 20) * 1) + top)

time.sleep(1)
pg.hotkey("enter")
pg.hotkey("tab")
pg.typewrite("1")
pg.hotkey("enter")
pg.hotkey("down")
pg.hotkey("enter")
# 移动窗口  
win32gui.SetWindowPos(hwnd, win32con.HWND_TOP, new_left, new_top, 551, 451, 0)
pg.click((right - left) * (1 * (10 / 13)) + left, (bottom - top) * ((18.5 / 20) * 1) + top)
pg.hotkey("enter")
time.sleep(0.5)
pg.hotkey("enter")
time.sleep(0.5)
pg.hotkey("enter")


# success_hwnd = win32gui.FindWindow(None, "成功")
# while True :
#     time.sleep(5)
#     # 导出成功
#     success_hwnd = win32gui.FindWindow(None, "成功")
#     if success_hwnd:
#         app = Application(backend='uia').connect(handle=success_hwnd)
#         dlg = app.window(title=win32gui.GetWindowText(success_hwnd))  
#         print(dlg.print_ctrl_ids)
# dlg = app.window(title=win32gui.GetWindowText(tips))  # 根据你的窗口标题进行调整  
        # elec = hwnd_child  
# new = win32gui.FindWindow(None, "生成标书")


# hwnd = win32gui.FindWindow(None, "斯维尔清单计价 TH-BQ2016 学习版")
# app = Application(backend='uia').connect(handle=hwnd)
# dlg = app.window(title=win32gui.GetWindowText(hwnd))  # 根据你的窗口标题进行调整  
# print(dlg.print_ctrl_ids())








# if hwnd:
#     # win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)
#     print(f"窗口句柄为：{hwnd}")
#     # win32gui.SendMessage(hwnd, win32con.WM_CLOSE, 0, 0)
# else:
#     print("未找到指定窗口")

# app = Application(backend='uia').connect(handle=hwnd)
# dlg = app.window(title=win32gui.GetWindowText(hwnd))  # 根据你的窗口标题进行调整  
# print(dlg.print_control_identifiers())


# hwnd = win32gui.FindWindow(None, "导出为辅助评标标准接口文件")
# if hwnd:
#     # win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)
#     print(f"窗口句柄为：{hwnd}")
#     # win32gui.SendMessage(hwnd, win32con.WM_CLOSE, 0, 0)
# else:
#     print("未找到指定窗口")

# app = Application(backend='uia').connect(handle=hwnd)
# dlg = app.window(title=win32gui.GetWindowText(hwnd))  # 根据你的窗口标题进行调整  
# print(dlg.print_control_identifiers())

# button = dlg.child_window(title="控制价文件", control_type="RadioButton")
# button.click_input()



# menu = win32gui.GetMenu(hwnd)
# menu1 = win32gui.GetSubMenu(menu, 0)#第几个菜单
# cmd_ID = win32gui.GetMenuItemID(menu1, 1)#第几个子菜单
# win32gui.PostMessage(hwnd, win32con.WM_COMMAND, cmd_ID, 0)
# time.sleep(2)
# open = win32gui.FindWindow(None, "打开")
# if open:
#     # win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)
#     print(f"打开窗口句柄为：{open}")
#     time.sleep(1)
# else:
#     print("打开未找到指定窗口")
# child_windows = []  
# win32gui.EnumChildWindows(open, enum_child_windows, child_windows)  
# print(len(child_windows))
# 假设我们找到了一个类名为 'EDIT' 的子窗口（通常编辑框的类名）  
# edit_hwnd = None  
# confirm_hwnd = None
# for hwnd_child, window_text_child in child_windows:  
#     # print(str(hwnd_child) + " " + win32gui.GetClassName(hwnd_child) + " " + win32gui.GetWindowText(hwnd_child))
#     if win32gui.GetClassName(hwnd_child) == 'Edit':  
#         edit_hwnd = hwnd_child  
# win32gui.SendMessage(edit_hwnd, win32con.WM_SETTEXT, 0, target_file_path)
# setForcus(open)
# pg.hotkey('Enter')
# # win32gui.SetFocus(open)
# time.sleep(3)